{
  "nbformat": 4,
  "nbformat_minor": 0,
  "metadata": {
    "colab": {
      "provenance": []
    },
    "kernelspec": {
      "name": "python3",
      "display_name": "Python 3"
    },
    "language_info": {
      "name": "python"
    }
  },
  "cells": [
    {
      "cell_type": "markdown",
      "source": [
        "二、从二维数组看起（axis=0 和 axis=1）"
      ],
      "metadata": {
        "id": "Hv8gBbcwYB3p"
      }
    },
    {
      "cell_type": "code",
      "execution_count": 15,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "W-ykf4arXj22",
        "outputId": "a3350bde-8876-42b9-edf3-dbf07b9365f4"
      },
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "(2, 3)\n",
            "2\n"
          ]
        }
      ],
      "source": [
        "import numpy as np\n",
        "\n",
        "a = np.array([[1, 2, 3],\n",
        "        [4, 5, 6]])\n",
        "print(a.shape)\n",
        "print(a.ndim)"
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
        "1、沿 axis=0 求和（跨行）："
      ],
      "metadata": {
        "id": "_VbsMoLBYGdS"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "axis = 0\n",
        "sum_axis = np.sum(a, axis=axis)\n",
        "print(sum_axis)\n",
        "print(sum_axis.shape)\n",
        "print(sum_axis.ndim)"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "Cg-sipFfYH6Y",
        "outputId": "9180a092-f33c-4570-b19b-1fe5470084a6"
      },
      "execution_count": 16,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "[5 7 9]\n",
            "(3,)\n",
            "1\n"
          ]
        }
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
        "2、沿 axis=1 求和（跨列）："
      ],
      "metadata": {
        "id": "0DzTJDr_YZ_3"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "axis = 1\n",
        "sum_axis = np.sum(a, axis=axis)\n",
        "print(sum_axis)\n",
        "print(sum_axis.shape)\n",
        "print(sum_axis.ndim)"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "qRPImU_OYbkn",
        "outputId": "d9fc0fb4-4065-4af2-bf5c-463f4134ad0f"
      },
      "execution_count": 17,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "[ 6 15]\n",
            "(2,)\n",
            "1\n"
          ]
        }
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
        "三、进入三维世界：axis=0、1、2 的差别"
      ],
      "metadata": {
        "id": "kvfpJa6SYwKg"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "a = np.array([\n",
        "    [[1, 2, 3],\n",
        "    [4, 5, 6]],\n",
        "\n",
        "    [[7, 8, 9],\n",
        "    [10, 11, 12]]\n",
        "  ])\n",
        "print(a.shape)\n",
        "print(a.ndim)"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "War7qa8xYw76",
        "outputId": "62f89f17-3902-40fb-cc6f-418f9986b5a3"
      },
      "execution_count": 18,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "(2, 2, 3)\n",
            "3\n"
          ]
        }
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
        "1、沿 axis=0 求和（跨“矩阵”）："
      ],
      "metadata": {
        "id": "BHbEYwHIZG1Z"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "axis = 0\n",
        "sum_axis = np.sum(a, axis=axis)\n",
        "print(sum_axis)\n",
        "print(sum_axis.shape)\n",
        "print(sum_axis.ndim)"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "SixEr_k1ZHfn",
        "outputId": "ff0543f4-f49d-4b35-bef1-8513d8f4ef52"
      },
      "execution_count": 19,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "[[ 8 10 12]\n",
            " [14 16 18]]\n",
            "(2, 3)\n",
            "2\n"
          ]
        }
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
        "2、沿 axis=1 求和（跨“行”）："
      ],
      "metadata": {
        "id": "7a2vzCgsZdIR"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "axis = 1\n",
        "sum_axis = np.sum(a, axis=axis)\n",
        "print(sum_axis)\n",
        "print(sum_axis.shape)\n",
        "print(sum_axis.ndim)"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "rsw5FwBRZfjg",
        "outputId": "f645b1fd-462f-4630-a605-554ed2ebc09b"
      },
      "execution_count": 20,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "[[ 5  7  9]\n",
            " [17 19 21]]\n",
            "(2, 3)\n",
            "2\n"
          ]
        }
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
        "3、沿 axis=2 求和（跨“列”）："
      ],
      "metadata": {
        "id": "dwCvZAEPZjuY"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "axis = 2\n",
        "sum_axis = np.sum(a, axis=axis)\n",
        "print(sum_axis)\n",
        "print(sum_axis.shape)\n",
        "print(sum_axis.ndim)"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "G6RUVavYZjV4",
        "outputId": "87c13edd-1230-4d6b-fadf-65da1d3194be"
      },
      "execution_count": 21,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "[[ 6 15]\n",
            " [24 33]]\n",
            "(2, 2)\n",
            "2\n"
          ]
        }
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
        "四、四维数组求和（axis=0、1、2、3 全细节）"
      ],
      "metadata": {
        "id": "88PZMeSKZptP"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "a = np.array([\n",
        "    [\n",
        "      [[1, 2], [3, 4]],\n",
        "      [[5, 6], [7, 8]]\n",
        "    ],\n",
        "\n",
        "    [\n",
        "      [[9, 10], [11, 12]],\n",
        "      [[13, 14], [15, 16]]\n",
        "    ]\n",
        "  ])\n",
        "print(a.shape)\n",
        "print(a.ndim)"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "8AlFf4hGZ5M4",
        "outputId": "e0361037-9703-4504-efe5-8e7ba1834122"
      },
      "execution_count": 24,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "(2, 2, 2, 2)\n",
            "4\n"
          ]
        }
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
        "1、沿 axis=0 求和：跨 batch 相加"
      ],
      "metadata": {
        "id": "2Wf38UmmajnI"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "axis = 0\n",
        "sum_axis = np.sum(a, axis=axis)\n",
        "print(sum_axis)\n",
        "print(sum_axis.shape)\n",
        "print(sum_axis.ndim)"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "skq9frXBa4KI",
        "outputId": "e8db9d47-2bee-4db0-c25e-3ab319c6dd2b"
      },
      "execution_count": 25,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "[[[10 12]\n",
            "  [14 16]]\n",
            "\n",
            " [[18 20]\n",
            "  [22 24]]]\n",
            "(2, 2, 2)\n",
            "3\n"
          ]
        }
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
        "2、沿 axis=1 求和：跨深度（第二层）求和"
      ],
      "metadata": {
        "id": "2e5x477WakMg"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "axis = 1\n",
        "sum_axis = np.sum(a, axis=axis)\n",
        "print(sum_axis)\n",
        "print(sum_axis.shape)\n",
        "print(sum_axis.ndim)"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "Qqht594ma5Tv",
        "outputId": "98587b79-968a-4784-c891-53a54303afc0"
      },
      "execution_count": 26,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "[[[ 6  8]\n",
            "  [10 12]]\n",
            "\n",
            " [[22 24]\n",
            "  [26 28]]]\n",
            "(2, 2, 2)\n",
            "3\n"
          ]
        }
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
        "3、沿 axis=2 求和：压缩行"
      ],
      "metadata": {
        "id": "Vj2tb3FvamdH"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "axis = 2\n",
        "sum_axis = np.sum(a, axis=axis)\n",
        "print(sum_axis)\n",
        "print(sum_axis.shape)\n",
        "print(sum_axis.ndim)"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "FVIGmGs2a69w",
        "outputId": "c4f2f048-a0be-485c-b6bf-312aa728dce5"
      },
      "execution_count": 27,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "[[[ 4  6]\n",
            "  [12 14]]\n",
            "\n",
            " [[20 22]\n",
            "  [28 30]]]\n",
            "(2, 2, 2)\n",
            "3\n"
          ]
        }
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
        "4、沿 axis=3 求和：压缩通道"
      ],
      "metadata": {
        "id": "dPedq2PkayuW"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "axis = 3\n",
        "sum_axis = np.sum(a, axis=axis)\n",
        "print(sum_axis)\n",
        "print(sum_axis.shape)\n",
        "print(sum_axis.ndim)"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "TweXYWlpa8g_",
        "outputId": "61844cd5-14c2-41fd-dc22-49ae6cc97ef0"
      },
      "execution_count": 28,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "[[[ 3  7]\n",
            "  [11 15]]\n",
            "\n",
            " [[19 23]\n",
            "  [27 31]]]\n",
            "(2, 2, 2)\n",
            "3\n"
          ]
        }
      ]
    }
  ]
}